package com.cheyunkeji.er.view.evaluate;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael on 2017/4/11 17:17.
 */

public class EvaluateCheckItemView2 extends RelativeLayout implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private static final String TAG = EvaluateCheckItemView2.class.getSimpleName();

    @BindView(R.id.tv_check_item_desc)
    TextView tvCheckItemDesc;
    @BindView(R.id.tv_index_number)
    TextView tvIndexNumber;
    @BindView(R.id.cb_none)
    CheckBox cbNone;
    @BindView(R.id.cb_lite)
    CheckBox cbLite;
    @BindView(R.id.cb_heavy)
    CheckBox cbHeavy;
    @BindView(R.id.cb_need_fix)
    CheckBox cbNeedFix;
    @BindView(R.id.ib_take_photo)
    ImageButton ibTakePhoto;
    @BindView(R.id.et_remarks)
    EditText etRemarks;
    @BindView(R.id.rl_content)
    RelativeLayout rootView;

    CheckBox[] boxs;

    //  当前选择项下标值，默认为 -1
    private int checkItemIndexNumber = -1;

    private OnPhotoIconClicked listener;

    /**
     * 当前选中CheckBox下标，默认为4 表示没选中
     */
    private int currentCheckedIndex = 4;


    public String getImgPath() {
        return imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }


    //    照片路径
    private String imgPath = "" ;

    public EvaluateCheckItemView2(Context context) {
        super(context);
        initView(context);
    }

    public EvaluateCheckItemView2(Context context, AttributeSet attrs) {
        super(context, attrs);

        rootView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.er_evaluate_check_view_layout2, this, true);
        ButterKnife.bind(this, rootView);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EvaluateCheckItemView);

        for (int i = 0; i < typedArray.getIndexCount(); i++) {

            switch (typedArray.getIndex(i)) {
                case R.styleable.EvaluateCheckItemView_evaluate_check_bkg_color:
                    rootView.setBackgroundColor(typedArray.getColor(typedArray.getIndex(i), Color.parseColor("#00000000")));
                    break;
                case R.styleable.EvaluateCheckItemView_evaluate_check_desc:
                    tvCheckItemDesc.setText(typedArray.getString(typedArray.getIndex(i)));
                    break;
                case R.styleable.EvaluateCheckItemView_evaluate_check_index_number:
                    checkItemIndexNumber = Integer.valueOf(typedArray.getString(typedArray.getIndex(i)));
                    tvIndexNumber.setText(typedArray.getString(typedArray.getIndex(i)));
                    break;
                case R.styleable.EvaluateCheckItemView_evaluate_check_text_desc_type :

                    if(typedArray.getInt(typedArray.getIndex(i),1) == 2){

                        cbLite.setText("缺件");
                        cbHeavy.setText("损坏");
                        cbNeedFix.setText("可修理");

                    }

                    break ;

                default:
                    break;
            }
        }

        typedArray.recycle();

        initView(context);

    }

    public EvaluateCheckItemView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public int getCheckedIndex() {
        return currentCheckedIndex;
    }

    private void initView(Context context) {

        boxs = new CheckBox[]{cbNone, cbLite, cbHeavy, cbNeedFix};

        for (int i = 0; i < boxs.length; i++) {

            boxs[i].setOnCheckedChangeListener(this);
        }

        ibTakePhoto.setOnClickListener(this);
    }


    public void setOnPhotoIconClickedListener(OnPhotoIconClicked listener) {
        this.listener = listener;
    }


    /**
     * 选中某项
     *
     * @param index
     */
    public void setCheckedIndex(int index) {

        if (4 == index) {
            for (int i = 0; i < boxs.length; i++) {
                if (boxs[i].isChecked())
                    boxs[i].setChecked(false);
            }
        } else {
            boxs[index].setChecked(true);
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {

            for(int i = 0 ;i < boxs.length ;i++){
                if(buttonView == boxs[i]){
                    currentCheckedIndex = i ;
                }
            }

            for(int i = 0 ; i < boxs.length ; i++){
                if(currentCheckedIndex != i && boxs[i].isChecked()){
                    boxs[i].setChecked(false);
                }
            }
            listener.onItemChecked(currentCheckedIndex,checkItemIndexNumber);

        } else {
            if (buttonView == boxs[currentCheckedIndex]) {
                currentCheckedIndex = 4 ;
                listener.onItemChecked(currentCheckedIndex,checkItemIndexNumber);
            }
        }
//        Log.e(TAG, "onCheckedChanged: " + currentCheckedIndex);

    }


    @Override
    public void onClick(View v) {
        if(null != listener)
            listener.takePhoto(checkItemIndexNumber);
    }

    public interface OnPhotoIconClicked {

        void takePhoto(int evaluateItemIndex);

        void onItemChecked(int cbIndex,int evaluateItemIndex);
    }


    /**
     * 设置拍照按钮状态
     * @param hasPhoto
     */
    public void setPhotoBtnState(boolean hasPhoto){
        if(hasPhoto){
            ibTakePhoto.setBackgroundResource(R.mipmap.er_icon_take_photo_pressed);
        }else{
            ibTakePhoto.setBackgroundResource(R.mipmap.er_icon_take_photo_normal);
        }
    }


    /**
     * 设置备注说明
     * @param remarksDesc
     */
    public void setRemarksDesc(String remarksDesc){
        etRemarks.setText(remarksDesc);
    }


    /**
     * 获取备注说明
     * @return
     */
    public String getRemarkDesc(){
        return etRemarks.getText().toString().trim() ;
    }

}
